package example05;

public class Ticket implements Runnable {
    private int tickets = 100;

    public void run() {
        while (true) {
            synchronized (this) {
                try {
                    if (tickets > 0) {
                        Thread.sleep(10);
                        String name = Thread.currentThread().getName();
                        System.out.println(name + "正在发售第" + tickets-- + "车票");
                    } else {
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
